﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.JuicyLib.Services;
using System.IO;
using System.Threading;
using Algobox.JuicyLib.Net.FTP;


namespace Algobox.JuicyLib.Apps.StrikeImporter
{
    public class StrikeImporterManager : ServiceManager
    {
        /// <summary>
        /// class to process csv file and insert into db
        /// </summary>
        StrikeImporter importHandler = null;

        /// <summary>
        /// class to handle ftp connection/work
        /// </summary>
        Algobox.JuicyLib.Net.FTP.JuicyFTP ftpHandler = null;

        /// <summary>
        /// Custom constructor
        /// </summary>
        /// <param name="ftpUser">user credentials - username for ftp access</param>
        /// <param name="ftpPassword">user credentials - password for ftp access</param>
        /// <param name="ftpPURI">ftp source</param>
        /// <param name="dbConnectionString">dsn string to SQL db to write data to</param>
        public StrikeImporterManager(
            string ftpUser, 
            string ftpPassword,
            string ftpURI,
            string ftpBackupDir,
            string dbConnectionString
            )
            : base()
        {
            importHandler = new StrikeImporter(dbConnectionString);
            ftpHandler = new JuicyFTP(ftpUser, ftpPassword, ftpURI, ftpBackupDir, JuicyFTP.FTPMode.Windows, JuicyFTP.FTPCommand.Passive);
        }


        public StrikeImporterManager(
            string ftpUser,
            string ftpPassword,
            string ftpURI,
            string ftpBackupDir,
            string dbConnectionString,
            string threadName            
            )
            : base(threadName)
        {
            importHandler = new StrikeImporter(dbConnectionString);
            ftpHandler = new JuicyFTP(ftpUser, ftpPassword, ftpURI, ftpBackupDir, JuicyFTP.FTPMode.Windows, JuicyFTP.FTPCommand.Passive);
        }


        /// <summary>
        /// main application function to process all data
        /// </summary>
        public void ProcessData()
        {
            List<string> fileList = null;

            // used during testing
            //Thread.Sleep(60000);
            bool result = false;

            if (ftpHandler.TryGetFileList(out fileList))
            {
                StreamReader reader = null;
                
                foreach (string fileName in fileList)
                {
                    // check that we want this file first
                    if (importHandler.FilterFileName(fileName))
                    {
                        // go fetch this file
                        if (ftpHandler.GetFileAsStreamReader(fileName, out reader))
                        {
                            // give the importer the reader stream to the current file
                            importHandler.ProcessStream(reader);

                            // close the open file stream to release ftp connection
                            reader.Close();

                            // backup file
                            result = ftpHandler.TryBackupFile(fileName);

                            if(!result)
                                importHandler.Log("Failed to backup file: " + fileName);

                            ////// BACKUP FILES FROM NOW ON /////
                            // delete the file
                            //ftpHandler.DeleteFile(fileName); // handle when this fails
                        }
                        else
                        {
                            // failed to read file
                            importHandler.Log("Failed to read file " + fileName);
                        }
                    }
                }
            }


        }


        public override void Process()
        {
            // lets do it our way
            base.Stop();

            workerThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.ProcessData));

            workerThread.Name = base.threadName;

            workerThread.Start();


        }


    }
}
